/*
 * @Author: CodeHare
 * @Date: 2023-11-30 16:45:54
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-01-05 09:56:07
 * @Description:
 */
import 'dart:collection';

import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/model/device_obj/spec/array_spec.dart';
import 'package:yt_devops/model/device_obj/spec/double_spec.dart';
import 'package:yt_devops/model/device_obj/spec/struct_spec.dart';
import 'package:yt_devops/model/device_obj/spec/time_range_spec.dart';

part 'data_type_model.g.dart';

@JsonSerializable()
class DataTypeModel {
  DataTypeEnum type;

  late Object specs;

  DataTypeModel({required this.type, Object? spec}) {
    if (type == DataTypeEnum.double ||
        type == DataTypeEnum.long ||
        type == DataTypeEnum.int) {
      specs = DoubleSpec.fromJson(spec as Map<String, dynamic>);
    } else if (type == DataTypeEnum.timeRange) {
      specs = TimeRageSpec.fromJson(spec as Map<String, dynamic>);
    } else if (type == DataTypeEnum.array) {
      specs = ArraySpec.fromJson(spec as Map<String, dynamic>);
    } else if (type == DataTypeEnum.struct) {
      specs = StructSpec.fromJson(spec as List);
    } else {
      specs = spec!;
    }
  }

  factory DataTypeModel.fromJson(Map<String, dynamic> json) =>
      _$DataTypeModelFromJson(json);

  Map<String, dynamic> toJson() => _$DataTypeModelToJson(this);
}

@JsonEnum(valueField: 'label')
enum DataTypeEnum {
  double('double'),
  struct('struct'),
  array('array'),
  enumType('enum'),
  string('string'),
  timeRange('timeRange'),
  boolean('boolean'),
  int('int'),
  timestamp('timestamp'),
  long('long');

  final String label;
  const DataTypeEnum(this.label);
}
